<?php

class EntryController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl',
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array('vote'),
				'users'=>array('@'),
			),
			array('deny', // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Default action.
	 * @throws CHttpException when called
	 */
	public function actionIndex()
	{
		throw new CHttpException(404,Yii::t('frontend','The page your requested could not be found.'));
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model = $this->loadModel($id);
		
		$comment = new Comment();

		if (isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		if (isset($_POST['Comment']))
		{
			$comment->attributes = $_POST['Comment'];
			$comment->body = Yii::app()->bbcodeParser->bbcode2html($comment->body);
			$comment->entryId = $model->id;
			$comment->status = Comment::STATUS_NORMAL;
			
			if ($comment->save())
			{
				Yii::app()->user->setFlash(WebUser::FLASH_SUCCESS,Yii::t('flash','Comment added.'));
				$this->redirect(array('view','id'=>$id,'#'=>'comment-'.$comment->id));
			}

			Yii::app()->user->setFlash(WebUser::FLASH_ERROR,Yii::t('flash','Adding comment failed!'));
		}

		$comment->body = Yii::app()->bbcodeParser->html2bbcode($comment->body);

		$comments = new CActiveDataProvider('Comment', array(
			'criteria'=>array(
				'condition'=>'entryId=:entryId AND status IN (:status) AND deleted=0', // we need the deleted here because beforeFind does not work here
				'params'=>array(
					':entryId'=>$model->id,
					':status'=>Comment::STATUS_NORMAL.','.Comment::STATUS_REPORTED
				),
			),
			'sort'=>array(
				'attributes'=>array(
					'ups'=>array(
						'label'=>Yii::t('frontend','Most Liked'),
						'default'=>'desc',
					),
					'created'=>array(
						'label'=>Yii::t('frontend','Oldest'),
						'asc'=>'t.created DESC',
						'desc'=>'t.created ASC',
						'default'=>'desc',
					),
				),
				'defaultOrder'=>array(
					'created'=>true,
				),
			),
			'pagination'=>array(
				'pageSize'=>20, // TODO: Implement some reasonable paging, e.g. Facebook style
			),
		));

		$this->render('view',array(
			'model'=>$model,
			'comment'=>$comment,
			'comments'=>$comments,
			'prevId'=>$model->prevId,
			'nextId'=>$model->nextId,
		));
	}

	/**
	 * Adds a vote for a particular model.
	 * @param $id the ID of the model to be voted
	 */
	public function actionVote($id)
	{
		$model = $this->loadModel($id);

		if (!$model->contest->isVotingAllowed() || $model->hasVoted(Yii::app()->user->id))
		{
			Yii::app()->user->setFlash(WebUser::FLASH_ERROR,Yii::t('flash','Voting is not allowed for this contest'));
			$this->redirect(array('view','id'=>$model->id));
		}

		$vote = Vote::model()->findByAttributes(array(
			'voterId'=>Yii::app()->user->id,
			'entryId'=>$id,
			'contestId'=>$model->contestId,
		));

		if ($vote === null)
		{
			$vote = new Vote();
			$vote->voterId = Yii::app()->user->id;
			$vote->entryId = $model->id;
			$vote->contestId = $model->contestId;
			$vote->save();

			$model->votes++;
			$model->save(false);

			Yii::app()->user->setFlash(WebUser::FLASH_SUCCESS,Yii::t('flash','Vote registered.'));
		}

		$this->redirect(array('entry/view', 'id'=>$id));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Entry the model
	 */
	public function loadModel($id)
	{
		$model=Entry::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax'] === 'entry-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
